/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.Login;
import model.entities.Role;


/**
 *
 * @author Hieu.Nguyen
 */
public class LoginDAO {
    private static LoginDAO loginDAO;

    public static LoginDAO getInstance() {
        if (loginDAO == null) {
            loginDAO = new LoginDAO();
        }
        return loginDAO;
    }
    
     public List<Login> getLogin(boolean isActived) {
        List<Login> listLogin = new ArrayList<Login>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblLogin where isActived = 'true'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Login r = new Login();
                r.setUserID(rs.getInt("UserID"));
                r.setUserName(rs.getString("UserName"));
                r.setPassword(rs.getString("Password"));
                
                Role aa = RoleDAO.getInstance().getRoleByID(rs.getInt("RoleID"));
                r.setRoleName(aa);
                
                r.setUserFirstName(rs.getString("UserFirstName"));
                r.setUserMiddleName(rs.getString("UserMiddleName"));
                r.setUserLastName(rs.getString("UserLastName"));
                r.setUserContactInfo(rs.getString("UserContactInfo"));
                listLogin.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listLogin;
    }
     public boolean createLogin(Login login) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblLogin  values (?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
           
            pstmt.setString(1, login.getUserName());
            pstmt.setString(2, login.getPassword());
            pstmt.setInt(3, login.getRoleName().getRoleID());
            pstmt.setString(4, login.getUserFirstName());
            pstmt.setString(5, login.getUserMiddleName());
            pstmt.setString(6, login.getUserLastName());
            pstmt.setString(7, login.getUserContactInfo());
            pstmt.setBoolean(8, login.isIsActived());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
     }
     public Login getLoginByID(int loginID) {

        Login li = new Login();
        RoleDAO rl= new RoleDAO();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblLogin where UserID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, loginID);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                
                li.setUserID(rs.getInt("UserID"));
                li.setUserName(rs.getString("UserName"));
                li.setPassword(rs.getString("Password"));
                li.setRoleName(rl.getRoleByID(Integer.parseInt(rs.getString("RoleID"))));
                li.setUserFirstName(rs.getString("UserFirstName"));
                li.setUserMiddleName(rs.getString("UserMiddleName"));
                li.setUserLastName(rs.getString("UserLastName"));
   
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return li;
     }
      public boolean deleteUser(int userID) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblLogin set isActived = 'false' where UserID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userID);

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }
      public boolean updateLogin(Login login) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblLogin set UserName = ?, Password = ?, RoleID = ?, UserFirstName = ?, UserMiddleName = ?, UserLastName = ?, UserContactInfo = ? where UserID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, login.getUserName());
            pstmt.setString(2, login.getPassword());
            pstmt.setInt(3, login.getRoleName().getRoleID());
            pstmt.setString(4, login.getUserFirstName());
            pstmt.setString(5, login.getUserMiddleName());
            pstmt.setString(6, login.getUserLastName());
            pstmt.setString(7, login.getUserContactInfo());
            pstmt.setInt(8, login.getUserID());

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
    }
       public boolean checkLogin(String username, String pass) {
        boolean isCheck = false;
        List<Login> listUser = new ArrayList<Login>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select UserID, UserName, Password from tblLogin where isActived = 'true'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Login login = new Login();
                login.setUserID(rs.getInt("userID"));
                login.setUserName(rs.getString("userName"));
                login.setPassword(rs.getString("Password"));
                listUser.add(login);
            }
            for (Login login : listUser) {
                if ((login.getUserName().equals(username)) && (login.getPassword().equals(pass))) {
                    isCheck = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }
}

